package com.example.mechrevo.a3dtest;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.Scroller;


public class MyGLSurfaceView extends GLSurfaceView {


    private float dx ,dy;
    private RenderView myRenderer ;
    private GestureDetector detector;

    public MyGLSurfaceView(Context context) {
        this(context,null);
    }

    public MyGLSurfaceView(Context context, AttributeSet attrs) {
        super(context,attrs);
        init();
    }


    public void setMyRenderer(RenderView myRenderer) {
        this.myRenderer = myRenderer;
        setRenderer(myRenderer);
    }

    private void init() {
        setEGLConfigChooser(8, 8, 8, 8, 16, 0);
        getHolder().setFormat(PixelFormat.TRANSLUCENT);
        setZOrderOnTop(true);
        detector = new GestureDetector(getContext(),new MyGestureDetector());
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                dx = event.getX();
                dy = event.getY();
                break;

            case MotionEvent.ACTION_MOVE:
                float mx = event.getX() - dx;
                float my = event.getY() - dy;

                if (Math.abs(mx) > ViewConfiguration.getTouchSlop()) {
                    if (getCurrentAnge(event) < 45) {
                        if (mx > 0) {
                            myRenderer.applyRotationY( -0.1f);
                            Orientation = 0;
                        } else if (mx < 0) {
                            Orientation = 1;
                            myRenderer.applyRotationY(0.1f);
                        }
                    }
                }

                if (Math.abs(my) > ViewConfiguration.getTouchSlop()) {
                    if (getCurrentAnge(event) > 45) {
                        if (my > 0) {
                            Orientation = 2;
                            myRenderer.applyRotationX(-0.1f);
                        } else if (my < 0) {
                            Orientation = 3;
                            myRenderer.applyRotationX(0.1f);
                        }
                    }
                }
                break;

            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                dx = 0;
                break;
        }
        detector.onTouchEvent(event);
        return true;
    }



    /**
     *
     * 获取当前手指滑动的角度
     * @param event
     * @return
     */
    public double getCurrentAnge(MotionEvent event){
        double ange = Math.toDegrees(Math.atan2(Math.abs(event.getY() - dy), Math.abs(event.getX() - dx)));
        Log.v("=======ange========","ange="+ange);
        return  ange;
    }


    int Orientation ;
    Scroller scroller = new Scroller(getContext()) ;

    public class MyGestureDetector implements GestureDetector.OnGestureListener{

        @Override
        public boolean onDown(MotionEvent e) {
            return false;
        }

        @Override
        public void onShowPress(MotionEvent e) {

        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {

        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Log.v("=====velocityX===","velocityX="+velocityX);
//            velocityX = velocityX > 2000 ? 2000 : velocityX;
//            velocityX = velocityX < -2000 ? -2000 : velocityX;
//            velocityY = velocityY > 2000 ? 2000 : velocityY;
//            velocityY = velocityY < -2000 ? 2000 : velocityY;
            float angleX = (velocityX / 2000);
            float angleY = (velocityY / 2000);
            if (Math.abs(angleX)>1||Math.abs(angleY)>1)
                animRotate();
            return true;
        }
    }

    private void animRotate() {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(1,0);
        valueAnimator.setDuration(1000);
        valueAnimator.start();
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float radio = (float) animation.getAnimatedValue();
                switch (Orientation){
                    case 0:
                        myRenderer.applyRotationY(-(float) (radio*0.1));
                        break;
                    case 1:
                        myRenderer.applyRotationY((float) (radio*0.1));
                        break;
                    case 2:
                        myRenderer.applyRotationX(-(float) (radio*0.1));
                        break;
                    case 3:
                        myRenderer.applyRotationX((float) (radio*0.1));
                        break;
                }
            }
        });

    }


    @Override
    public void computeScroll() {
        super.computeScroll();
        if (scroller.computeScrollOffset()){
            scrollTo(scroller.getCurrX(),scroller.getCurrY());
            myRenderer.applyRotationX(0.1f);
        }
    }
}
